// pages/category/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from "../../lib/runtime/runtime";
Page({
  data: {
    leftMenuList: [],
    rightContent: [],
    currentIndex: 0,
    scrollTop: 0
  },
  cateList: {},
  onLoad: function(options) {
    const Cates = wx.getStorageSync("cates");
    if (!Cates) {
      this.getCate();
    } else {
      if (Date.now() - Cates.time > 1000 * 10) {
        this.getCate();
      } else {
        this.cateList = Cates.data;
        let leftMenuList = this.cateList.map(v => v.cat_name);
        let rightContent = this.cateList[0].children;
        this.setData({
          leftMenuList,
          rightContent
        });
      }
      console.log(Cates);
    }
  },
  async getCate() {
    const res = await request({ url: "/categories" });
    this.cateList = res;
    wx.setStorageSync("cates", { time: Date.now(), data: this.cateList });

    let leftMenuList = this.cateList.map(v => v.cat_name);
    let rightContent = this.cateList[0].children;
    this.setData({
      leftMenuList,
      rightContent
    });
  },

  handleChangeMenu(e) {
    const { index: currentIndex } = e.currentTarget.dataset;
    const rightContent = this.cateList[currentIndex].children;
    this.setData({
      rightContent,
      currentIndex,
      scrollTop: 0
    });
  }
});
